home *** CD-ROM | disk | FTP | other *** search
/ Freelog 70 / Freelog070.iso / Internet / EasyPHP / easyphp1-8_setup.exe / {app} / phpmyadmin / libraries / mysql_charsets.lib.php < prev    next >
Encoding:
PHP Script  |  2004-11-19  |  13.5 KB  |  344 lines

  1. <?php
  2. /* $Id: mysql_charsets.lib.php,v 2.24 2004/11/19 14:04:02 garvinhicking Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5. if (PMA_MYSQL_INT_VERSION >= 40100){
  6.  
  7.     $res = PMA_DBI_query('SHOW CHARACTER SET;');
  8.  
  9.     $mysql_charsets = array();
  10.     while ($row = PMA_DBI_fetch_assoc($res)) {
  11.         $mysql_charsets[] = $row['Charset'];
  12.         $mysql_charsets_maxlen[$row['Charset']] = $row['Maxlen'];
  13.         $mysql_charsets_descriptions[$row['Charset']] = $row['Description'];
  14.     }
  15.     @PMA_DBI_free_result($res);
  16.     unset($res, $row);
  17.  
  18.     $res = PMA_DBI_query('SHOW COLLATION;');
  19.  
  20.     $mysql_charsets_count = count($mysql_charsets);
  21.     sort($mysql_charsets, SORT_STRING);
  22.  
  23.     $mysql_collations = array_flip($mysql_charsets);
  24.     $mysql_default_collations = $mysql_collations_flat = array();;
  25.     while ($row = PMA_DBI_fetch_assoc($res)) {
  26.         if (!is_array($mysql_collations[$row['Charset']])) {
  27.             $mysql_collations[$row['Charset']] = array($row['Collation']);
  28.         } else {
  29.             $mysql_collations[$row['Charset']][] = $row['Collation'];
  30.         }
  31.         $mysql_collations_flat[] = $row['Collation'];
  32.         if ((isset($row['D']) && $row['D'] == 'Y') || (isset($row['Default']) && $row['Default'] == 'Yes')) {
  33.             $mysql_default_collations[$row['Charset']] = $row['Collation'];
  34.         }
  35.     }
  36.  
  37.     $mysql_collations_count = count($mysql_collations_flat);
  38.     sort($mysql_collations_flat, SORT_STRING);
  39.     foreach ($mysql_collations AS $key => $value) {
  40.         sort($mysql_collations[$key], SORT_STRING);
  41.         reset($mysql_collations[$key]);
  42.     }
  43.  
  44.     @PMA_DBI_free_result($res);
  45.     unset($res, $row);
  46.  
  47.     function PMA_getCollationDescr($collation) {
  48.         static $collation_cache;
  49.  
  50.         if (!is_array($collation_cache)) {
  51.             $collation_cache = array();
  52.         } elseif (isset($collation_cache[$collation])) {
  53.             return $collation_cache[$collation];
  54.         }
  55.  
  56.         if ($collation == 'binary') {
  57.             return $GLOBALS['strBinary'];
  58.         }
  59.         $parts = explode('_', $collation);
  60.         if (count($parts) == 1) {
  61.             $parts[1] = 'general';
  62.         } elseif ($parts[1] == 'ci' || $parts[1] == 'cs') {
  63.             $parts[2] = $parts[1];
  64.             $parts[1] = 'general';
  65.         }
  66.         $descr = '';
  67.         switch ($parts[1]) {
  68.             case 'bulgarian':
  69.                 $descr = $GLOBALS['strBulgarian'];
  70.                 break;
  71.             case 'chinese':
  72.                 if ($parts[0] == 'gb2312' || $parts[0] == 'gbk') {
  73.                     $descr = $GLOBALS['strSimplifiedChinese'];
  74.                 } elseif ($parts[0] == 'big5') {
  75.                     $descr = $GLOBALS['strTraditionalChinese'];
  76.                 }
  77.                 break;
  78.             case 'ci':
  79.                 $descr = $GLOBALS['strCaseInsensitive'];
  80.                 break;
  81.             case 'cs':
  82.                 $descr = $GLOBALS['strCaseSensitive'];
  83.                 break;
  84.             case 'croatian':
  85.                 $descr = $GLOBALS['strCroatian'];
  86.                 break;
  87.             case 'czech':
  88.                 $descr = $GLOBALS['strCzech'];
  89.                 break;
  90.             case 'danish':
  91.                 $descr = $GLOBALS['strDanish'];
  92.                 break;
  93.             case 'english':
  94.                 $descr = $GLOBALS['strEnglish'];
  95.                 break;
  96.             case 'estonian':
  97.                 $descr = $GLOBALS['strEstonian'];
  98.                 break;
  99.             case 'german1':
  100.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strDictionary'] . ')';
  101.                 break;
  102.             case 'german2':
  103.                 $descr = $GLOBALS['strGerman'] . ' (' . $GLOBALS['strPhoneBook'] . ')';
  104.                 break;
  105.             case 'hungarian':
  106.                 $descr = $GLOBALS['strHungarian'];
  107.                 break;
  108.             case 'icelandic':
  109.                 $descr = $GLOBALS['strIcelandic'];
  110.                 break;
  111.             case 'japanese':
  112.                 $descr = $GLOBALS['strJapanese'];
  113.                 break;
  114.             case 'latvian':
  115.                 $descr = $GLOBALS['strLatvian'];
  116.                 break;
  117.             case 'lithuanian':
  118.                 $descr = $GLOBALS['strLithuanian'];
  119.                 break;
  120.             case 'korean':
  121.                 $descr = $GLOBALS['strKorean'];
  122.                 break;
  123.             case 'persian':
  124.                 $descr = $GLOBALS['strPersian'];
  125.                 break;
  126.             case 'polish':
  127.                 $descr = $GLOBALS['strPolish'];
  128.                 break;
  129.             case 'roman':
  130.                 $descr = $GLOBALS['strWestEuropean'];
  131.                 break;
  132.             case 'romanian':
  133.                 $descr = $GLOBALS['strRomanian'];
  134.                 break;
  135.             case 'slovak':
  136.                 $descr = $GLOBALS['strSlovak'];
  137.                 break;
  138.             case 'slovenian':
  139.                 $descr = $GLOBALS['strSlovenian'];
  140.                 break;
  141.             case 'spanish':
  142.                 $descr = $GLOBALS['strSpanish'];
  143.                 break;
  144.             case 'spanish2':
  145.                 $descr = $GLOBALS['strTraditionalSpanish'];
  146.                 break;
  147.             case 'swedish':
  148.                 $descr = $GLOBALS['strSwedish'];
  149.                 break;
  150.             case 'thai':
  151.                 $descr = $GLOBALS['strThai'];
  152.                 break;
  153.             case 'turkish':
  154.                 $descr = $GLOBALS['strTurkish'];
  155.                 break;
  156.             case 'ukrainian':
  157.                 $descr = $GLOBALS['strUkrainian'];
  158.                 break;
  159.             case 'unicode':
  160.                 $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  161.                 break;
  162.             case 'bin':
  163.                 $is_bin = TRUE;
  164.             case 'general':
  165.                 switch ($parts[0]) {
  166.                     // Unicode charsets
  167.                     case 'ucs2':
  168.                     case 'utf8':
  169.                         $descr = $GLOBALS['strUnicode'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  170.                         break;
  171.                     // West European charsets
  172.                     case 'ascii':
  173.                     case 'cp850':
  174.                     case 'dec8':
  175.                     case 'hp8':
  176.                     case 'latin1':
  177.                     case 'macroman':
  178.                         $descr = $GLOBALS['strWestEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  179.                         break;
  180.                     // Central European charsets
  181.                     case 'cp1250':
  182.                     case 'cp852':
  183.                     case 'latin2':
  184.                     case 'macce':
  185.                         $descr = $GLOBALS['strCentralEuropean'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  186.                         break;
  187.                     // Russian charsets
  188.                     case 'cp866':
  189.                     case 'koi8r':
  190.                         $descr = $GLOBALS['strRussian'];
  191.                         break;
  192.                     // Simplified Chinese charsets
  193.                     case 'gb2312':
  194.                     case 'gbk':
  195.                         $descr = $GLOBALS['strSimplifiedChinese'];
  196.                         break;
  197.                     // Japanese charsets
  198.                     case 'sjis':
  199.                     case 'ujis':
  200.                         $descr = $GLOBALS['strJapanese'];
  201.                         break;
  202.                     // Baltic charsets
  203.                     case 'cp1257':
  204.                     case 'latin7':
  205.                         $descr = $GLOBALS['strBaltic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  206.                         break;
  207.                     // Other
  208.                     case 'armscii8':
  209.                     case 'armscii':
  210.                         $descr = $GLOBALS['strArmenian'];
  211.                         break;
  212.                     case 'big5':
  213.                         $descr = $GLOBALS['strTraditionalChinese'];
  214.                         break;
  215.                     case 'cp1251':
  216.                         $descr = $GLOBALS['strCyrillic'] . ' (' . $GLOBALS['strMultilingual'] . ')';
  217.                         break;
  218.                     case 'cp1256':
  219.                         $descr = $GLOBALS['strArabic'];
  220.                         break;
  221.                     case 'euckr':
  222.                         $descr = $GLOBALS['strKorean'];
  223.                         break;
  224.                     case 'hebrew':
  225.                         $descr = $GLOBALS['strHebrew'];
  226.                         break;
  227.                     case 'geostd8':
  228.                         $descr = $GLOBALS['strGeorgian'];
  229.                         break;
  230.                     case 'greek':
  231.                         $descr = $GLOBALS['strGreek'];
  232.                         break;
  233.                     case 'keybcs2':
  234.                         $descr = $GLOBALS['strCzechSlovak'];
  235.                         break;
  236.                     case 'koi8u':
  237.                         $descr = $GLOBALS['strUkrainian'];
  238.                         break;
  239.                     case 'latin5':
  240.                         $descr = $GLOBALS['strTurkish'];
  241.                         break;
  242.                     case 'swe7':
  243.                         $descr = $GLOBALS['strSwedish'];
  244.                         break;
  245.                     case 'tis620':
  246.                         $descr = $GLOBALS['strThai'];
  247.                         break;
  248.                     default:
  249.                         $descr = $GLOBALS['strUnknown'];
  250.                         break;
  251.                 }
  252.                 if (!empty($is_bin)) {
  253.                     $descr .= ', ' . $GLOBALS['strBinary'];
  254.                 }
  255.                 break;
  256.             default: $descr = $GLOBALS['strUnknown'];
  257.         }
  258.         if (!empty($parts[2])) {
  259.             if ($parts[2] == 'ci') {
  260.                 $descr .= ', ' . $GLOBALS['strCaseInsensitive'];
  261.             } elseif ($parts[2] == 'cs') {
  262.                 $descr .= ', ' . $GLOBALS['strCaseSensitive'];
  263.             }
  264.         }
  265.  
  266.         $collation_cache[$collation] = $descr;
  267.         return $descr;
  268.     }
  269.  
  270.     function PMA_getDbCollation($db) {
  271.         global $userlink;
  272.         if (PMA_MYSQL_INT_VERSION >= 40101) {
  273.             // MySQL 4.1.0 does not support seperate charset settings
  274.             // for databases.
  275.             $res = PMA_DBI_query('SHOW CREATE DATABASE ' . PMA_backquote($db) . ';', NULL, PMA_DBI_QUERY_STORE);
  276.             $row = PMA_DBI_fetch_row($res);
  277.             PMA_DBI_free_result($res);
  278.             $tokenized = explode(' ', $row[1]);
  279.             unset($row, $res, $sql_query);
  280.  
  281.             for ($i = 1; $i + 3 < count($tokenized); $i++) {
  282.                 if ($tokenized[$i] == 'DEFAULT' && $tokenized[$i + 1] == 'CHARACTER' && $tokenized[$i + 2] == 'SET') {
  283.                     // We've found the character set!
  284.                     if (isset($tokenized[$i + 5]) && $tokenized[$i + 4] == 'COLLATE') {
  285.                         return $tokenized[$i + 5]; // We found the collation!
  286.                     } else {
  287.                         // We did not find the collation, so let's return the
  288.                         // default collation for the charset we've found.
  289.                         return $GLOBALS['mysql_default_collations'][$tokenized [$i + 3]];
  290.                     }
  291.                 }
  292.             }
  293.         }
  294.         return '';
  295.     }
  296.  
  297.     define('PMA_CSDROPDOWN_COLLATION', 0);
  298.     define('PMA_CSDROPDOWN_CHARSET',   1);
  299.  
  300.     function PMA_generateCharsetDropdownBox($type = PMA_CSDROPDOWN_COLLATION, $name = NULL, $id = NULL, $default = NULL, $label = TRUE, $indent = 0, $submitOnChange = FALSE) {
  301.         global $mysql_charsets, $mysql_charsets_descriptions, $mysql_collations;
  302.  
  303.         if (empty($name)) {
  304.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  305.                 $name = 'collation';
  306.             } else {
  307.                 $name = 'character_set';
  308.             }
  309.         }
  310.  
  311.         $spacer = '';
  312.         for ($i = 1; $i <= $indent; $i++) $spacer .= '    ';
  313.  
  314.         $return_str  = $spacer . '<select name="' . htmlspecialchars($name) . '"' . (empty($id) ? '' : ' id="' . htmlspecialchars($id) . '"') . ($submitOnChange ? ' onchange="this.form.submit();"' : '') . '>' . "\n";
  315.         if ($label) {
  316.             $return_str .= $spacer . '    <option value="">' . ($type == PMA_CSDROPDOWN_COLLATION ? $GLOBALS['strCollation'] : $GLOBALS['strCharset']) . '</option>' . "\n";
  317.         }
  318.         $return_str .= $spacer . '    <option value=""></option>' . "\n";
  319.         foreach ($mysql_charsets as $current_charset) {
  320.             $current_cs_descr = empty($mysql_charsets_descriptions[$current_charset]) ? $current_charset : $mysql_charsets_descriptions[$current_charset];
  321.             if ($type == PMA_CSDROPDOWN_COLLATION) {
  322.                 $return_str .= $spacer . '    <optgroup label="' . $current_charset . '" title="' . $current_cs_descr . '">' . "\n";
  323.                 foreach ($mysql_collations[$current_charset] as $current_collation) {
  324.                     $return_str .= $spacer . '        <option value="' . $current_collation . '" title="' . PMA_getCollationDescr($current_collation) . '"' . ($default == $current_collation ? ' selected="selected"' : '') . '>' . $current_collation . '</option>' . "\n";
  325.                 }
  326.                 $return_str .= $spacer . '    </optgroup>' . "\n";
  327.             } else {
  328.                 $return_str .= $spacer . '    <option value="' . $current_charset . '" title="' . $current_cs_descr . '"' . ($default == $current_charset ? ' selected="selected"' : '') . '>' . $current_charset . '</option>' . "\n";
  329.             }
  330.         }
  331.         $return_str .= $spacer . '</select>' . "\n";
  332.  
  333.         return $return_str;
  334.     }
  335.  
  336.     function PMA_generateCharsetQueryPart($collation) {
  337.         list($charset) = explode('_', $collation);
  338.         return ' CHARACTER SET ' . $charset . ($charset == $collation ? '' : ' COLLATE ' . $collation);
  339.     }
  340.  
  341. }
  342.  
  343. ?>
  344.